package com.novoda.runbuddy.activity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.DayControllerImpl;
import com.novoda.runbuddy.controller.IDayController;
import com.novoda.runbuddy.controller.IOptionsMenu;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.model.ListOfRuns;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;
import com.novoda.runbuddy.view.RunListAdapter;

public class Day extends ListActivity {

	private IDayController	dayOfRunDetailsController;
	private IOptionsMenu	optionsMenu;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.day);
		dayOfRunDetailsController = new DayControllerImpl(this);
		optionsMenu = new OptionsMenuImpl(this);

		Bundle bundle = this.getIntent().getExtras();
		Integer dayOfMonth = bundle.getInt(ActivityID.DAY_OF_MONTH);
		Integer monthOfYear = bundle.getInt(ActivityID.MONTH_OF_YEAR);
		Integer year = bundle.getInt(ActivityID.YEAR);

		TextView miles = new TextView(this);
		miles.setText("miles Run");
		TextView name = new TextView(this);
		name.setText("name of jog");
		TextView timerun = new TextView(this);
		timerun.setText("time");

		populateTodaysRunningEntries(dayOfRunDetailsController.getDayOfRuns(dayOfMonth, monthOfYear, year));
	}

	private void populateTodaysRunningEntries(ListOfRuns runEntries) {

		if (runEntries.size() > 0) {
			displayAllRunsTodayDetails(runEntries);
		}

	}

	private void displayAllRunsTodayDetails(ListOfRuns runEntries) {
		setListAdapter(new RunListAdapter(this, runEntries, dayOfRunDetailsController));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

}
